package genedit.gui;

import genedit.controller.interfaceActions.GCloseA;
import genedit.controller.interfaceActions.GNewDocumentA;
import genedit.controller.interfaceActions.GNewPackageA;
import genedit.controller.interfaceActions.GNewSubDocumentA;
import genedit.controller.interfaceActions.GSaveA;
import genedit.controller.interfaceActions.GSaveAsA;
import genedit.model.Document;
import genedit.model.Package;
import genedit.model.Project;
import genedit.model.Workspace;
import genedit.model.graph.GraphDocument;
import genedit.model.graph.GraphElement;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * Klasa zadužena za rad sa selekcijom u stablu.
 * 
 * @author Nikola
 * @version 1.1 18/12/13
 */
public class GTreeSelectionListener implements TreeSelectionListener {

	/**
	 * Metoda brine za omogućavanje i onemogućavanje određenih akcija na osnovu
	 * selektovanih elemenata. Takođe vodi računa da se svi selektovani elementi
	 * dijagrama selektuju i u samim dijagramima (uspostavljajući sinhronizaciju
	 * selekcije na radnoj površini i stablu).
	 */
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		JTree tree = (JTree) e.getSource();

		if (tree.hasFocus()) {
			GProjectTree.getInstance().clearSelectionInDocuments();

			TreePath[] selectedElements = tree.getSelectionPaths();
			if (selectedElements != null) {
				for (TreePath selectedElement : selectedElements) {
					Object element = selectedElement.getLastPathComponent();
					if (!(element instanceof GraphElement))
						continue;
					GraphElement graphElement = (GraphElement) element;
					((GraphDocument) (graphElement.getParent()))
							.addSelectedElementsFromTree(graphElement);
				}
			}
		}

		TreeNode node = (TreeNode) tree.getLastSelectedPathComponent();

		if (node == null || node instanceof Workspace || tree.getSelectionCount() > 1) {
			if (Workspace.getInstance().getActiveDocument() == null) {
				GSaveA.getInstance().setEnabled(false);
				GSaveAsA.getInstance().setEnabled(false);
				GCloseA.getInstance().setEnabled(false);
			} else {
				GSaveA.getInstance().setEnabled(true);
				GSaveAsA.getInstance().setEnabled(true);
				GCloseA.getInstance().setEnabled(true);
			}

			GNewDocumentA.getInstance().setEnabled(false);
			GNewSubDocumentA.getInstance().setEnabled(false);
			GNewPackageA.getInstance().setEnabled(false);
			return;
		} else {
			GSaveA.getInstance().setEnabled(true);
			GSaveAsA.getInstance().setEnabled(true);
			GCloseA.getInstance().setEnabled(true);
		}

		if (node instanceof Document) {
			if (node.getParent() instanceof Document) {
				// ako je selektovan poddokument zabrani kreiranje novog
				// dokumenta već samo dozvoli kreiranje poddokumenta
				GNewDocumentA.getInstance().setEnabled(false);
			} else {
				GNewDocumentA.getInstance().setEnabled(true);
			}
			GNewSubDocumentA.getInstance().setEnabled(true);
			GNewPackageA.getInstance().setEnabled(false);
			return;
		}
		if (node instanceof Package) {
			GNewDocumentA.getInstance().setEnabled(true);
			GNewPackageA.getInstance().setEnabled(true);
			GNewSubDocumentA.getInstance().setEnabled(false);
			return;
		}
		if (node instanceof Project) {
			GNewPackageA.getInstance().setEnabled(true);
			GNewDocumentA.getInstance().setEnabled(false);
			GNewSubDocumentA.getInstance().setEnabled(false);
			return;
		}

		if (node instanceof GraphElement) {
			GNewPackageA.getInstance().setEnabled(false);
			GNewDocumentA.getInstance().setEnabled(false);
			GNewSubDocumentA.getInstance().setEnabled(false);
			/*
			 * GraphDocumentView document =
			 * Workspace.getInstance().getActiveDocument(); if (document !=
			 * null) if
			 * (Workspace.getInstance().getActiveDocument().getModel().getId()
			 * .compareTo(((GraphDocument) node.getParent()).getId()) == 0) {
			 * 
			 * Iterator<ElementPainter> it =
			 * document.getIteratorElementPainters(); while (it.hasNext()) {
			 * ElementPainter element = it.next(); if
			 * (element.getElement().equals(node)) { if
			 * (!document.getSelectedElements().contains(element)) {
			 * document.addSelectedElements(element); } } }
			 * 
			 * }
			 */

		}
	}
}
